package
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	public class Table extends Sprite
	{
		public static const A5_HEIGHT:int = 420;
		public static const A5_WIDTH:int = 595;
		public static const X_MARGIN:int = 30;
		public static const Y_MARGIN:int = 30;
		public static const HANG_HEIGHT:int = 50;
		public static const LINE_SIZE:int = 1;
		
		public var print_x:int = 0;
		public var print_y:int = 0;
		public var tfTitle:TextField = new TextField();
		public var tfClientName:TextField = new TextField();
		public var tfData:TextField = new TextField();
		public var tfCangku:TextField = new TextField();
		public var tfPrint:TextField = new TextField();
		public var tfShuoMing:TextField = new TextField();
		public var tfShuoMing1:TextField = new TextField();
		public var data:Array = new Array();
		
		public function Table()
		{
			
		}
		
		public function makeTable(tableName:String, clientName:String, riqi:Array
				, colNames:Array, aligns:Array, colWidths:Array
				, zerenren:String, needClientPrint:Boolean, shuoming:String, data:Array):void
		{
			// 表头
			tfTitle.htmlText = "<b><font size='+6'>"+tableName+"</font></b>";
			tfTitle.autoSize = TextFieldAutoSize.CENTER;
			tfTitle.x = (A5_WIDTH - tfTitle.width)/2;
			tfTitle.y = Y_MARGIN;
			addChild(tfTitle);
			print_y += Y_MARGIN + tfTitle.height + 5;
			
			// 双下划线
			graphics.lineStyle(LINE_SIZE);
			graphics.moveTo(tfTitle.x + 2, tfTitle.y + tfTitle.height - 3);
			graphics.lineTo(tfTitle.x + tfTitle.width - 2, tfTitle.y + tfTitle.height - 3);
			graphics.moveTo(tfTitle.x + 2, tfTitle.y + tfTitle.height - 1);
			graphics.lineTo(tfTitle.x + tfTitle.width - 2, tfTitle.y + tfTitle.height - 1);
			
			// 客户名字
			tfClientName.htmlText = "<font size='+4'>客户：" + clientName+"</font>";
			tfClientName.autoSize = TextFieldAutoSize.LEFT;
			tfClientName.x = X_MARGIN;
			tfClientName.y = print_y;
			addChild(tfClientName);
			// 日期
			tfData.htmlText = "<font size='+4'>"+riqi[0]+"年"+riqi[1]+"月"+riqi[2]+"日</font>";
			tfData.autoSize = TextFieldAutoSize.RIGHT;
			tfData.x = A5_WIDTH - X_MARGIN - tfData.width;
			tfData.y = print_y;
			addChild(tfData);
			
			print_y += tfData.height;
			// 画线
			graphics.lineStyle(LINE_SIZE);
			
			var i:int = 0;
			var j:int = 0;
			// 横线
			for(i=0; i<5; ++i){
				graphics.moveTo(X_MARGIN, print_y+i*HANG_HEIGHT);
				graphics.lineTo(A5_WIDTH - X_MARGIN, print_y+i*HANG_HEIGHT);
			}
			
			// 纵向
			graphics.moveTo(X_MARGIN, print_y);
			graphics.lineTo(X_MARGIN, print_y+4*HANG_HEIGHT);
			print_x = X_MARGIN;
			for(i=0; i<colNames.length; ++i){
				graphics.moveTo(print_x + colWidths[i], print_y);
				graphics.lineTo(print_x + colWidths[i], print_y+4*HANG_HEIGHT);
				print_x += colWidths[i];
			}
			
			// 放置表头tf
			print_x = X_MARGIN;
			for(i=0; i<colNames.length; ++i){
				setTextField(print_x, print_y, colWidths[i], colNames[i], TextFormatAlign.CENTER);
				print_x += colWidths[i];
			}
			
			
			for(i=0; i<data.length; ++i){
				print_x = X_MARGIN;
				print_y += HANG_HEIGHT;
				
				for(j=0; j<colNames.length; ++j){
					setTextField(print_x, print_y, colWidths[j], data[i][j], aligns[j]);
					print_x += colWidths[j];
				}
			}
			
			print_x = X_MARGIN;
			print_y += (4-i)*HANG_HEIGHT+10;
			// 责任人
			tfCangku.htmlText = "<font size='+4'>仓库：" + zerenren+"</font>";
			tfCangku.autoSize = TextFieldAutoSize.LEFT;
			tfCangku.x = X_MARGIN;
			tfCangku.y = print_y;
			addChild(tfCangku);
			
			// 签章
			if(needClientPrint){
				tfPrint.htmlText = "<font size='+4'>收货单位签章： </font>";
				tfPrint.autoSize = TextFieldAutoSize.RIGHT;
				tfPrint.x = A5_WIDTH - X_MARGIN - tfPrint.width - 100;
				tfPrint.y = print_y;
				addChild(tfPrint);
			}
			
			print_y += tfCangku.height + 5;
			// 说明
			tfShuoMing.htmlText = "<font size='-1'>说明：</font>";
			tfShuoMing.autoSize = TextFieldAutoSize.LEFT;
			tfShuoMing.x = X_MARGIN;
			tfShuoMing.y = print_y;
			addChild(tfShuoMing);
			
			// 说明2
			tfShuoMing1.htmlText = "<font size='-1'>"+shuoming+" </font>";
			tfShuoMing1.multiline = true;
			tfShuoMing1.width = 500;
			tfShuoMing1.height = 60;
			tfShuoMing1.x = X_MARGIN+40;
			tfShuoMing1.y = print_y;
			addChild(tfShuoMing1);
		}
		
		private function setTextField(x:int, y:int, width:int=0, text:String=""
									  , align:String=TextFormatAlign.LEFT):TextField
		{
			var tf:TextField = new TextField();
			if(width==0){
				tf.autoSize = TextFieldAutoSize.LEFT;
			}
			else{
				tf.width = width;
			}
			tf.height = HANG_HEIGHT;
			tf.multiline = true;
			tf.wordWrap=true
			tf.x = x;
			tf.y = y+15;
			tf.text = text;
			
			var zimushu:int = 0;
			for(var i:int=0; i<tf.length; ++i){
				if(text.charCodeAt(i)<105){
					zimushu+=1;
				}
			}
			zimushu += 2*(text.length-zimushu);
			
			var format:TextFormat = new TextFormat();
			format.align = align;
			format.size = 16;
			if(tf.width/10<zimushu){
				format.size = 12;
				tf.y = y+18;
				if(zimushu>tf.width/7){
					tf.y = y+12;
				}
			}
//			if(text.length>15){
//				tf.y = y+10;
//			}
			tf.setTextFormat(format);
			
			addChild(tf);
			return tf;
		}
	}
}